package com.guan.friend.aspect;

import com.guan.common.core.constants.Constants;
import com.guan.common.core.enums.ResultCode;
import com.guan.common.core.utils.ThreadLocalUtil;
import com.guan.friend.domain.user.vo.UserVO;
import com.guan.friend.manager.UserCacheManager;
import com.guan.ojcommonsecurity.exception.ServiceException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    @Before(value = "@annotation(com.guan.friend.aspect.CheckUserStatus)")
    public void before(JoinPoint point){
        // 获取用户的信息 并 判断用户是否被拉黑
        Long userId = ThreadLocalUtil.get(Constants.TTL_KEY_USER_ID, Long.class);
        UserVO userCacheById = userCacheManager.getUserById(userId);
        if(userCacheById == null){
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Integer userStatus = userCacheById.getStatus();
        if(userStatus == 0){// == 0 表示已经被拉黑了， 抛出异常 停止服务
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }
}
